package br.com.hsp.system.beans;

import java.math.BigInteger;
import java.security.MessageDigest;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import br.com.hsp.system.repository.UserRepository;
import br.com.hsp.users.Usuario;

@ManagedBean
public class AutenticadorBean {
		
		private String usuarioCPF;
		private Usuario user;
		private String senha;
		private BigInteger senEnc;
		
		@ManagedProperty(value="#{userSessionBean}")
		private UserSessionBean session;
			
		
		public String autentica() {
			
			FacesContext fc = FacesContext.getCurrentInstance();
			EntityManager manager = this.getManager();
			UserRepository userRepository =  new UserRepository(manager);
			user = userRepository.procuraCPF(this.usuarioCPF);
			try {
				MessageDigest algorithm = MessageDigest.getInstance("MD5");
				senEnc = new  BigInteger(1, algorithm.digest(this.senha.getBytes()));
			} catch (Exception ex) {
				System.out.println(ex);
			}
			
			if(senEnc.toString().equals(user.getSenha()) ){
				ExternalContext ec = fc.getExternalContext();
				HttpSession sessionhttp = (HttpSession) ec.getSession(false);
				sessionhttp.setAttribute("usuarioCPF", this.usuarioCPF);
				this.session.setUserSession(user);
				this.setSenha(null);
				System.out.println(user.getClass().toString());
				if(user.getClass().toString().substring(23).contains("Paciente")) {
					sessionhttp.setAttribute("usuarioTipo", "1");
					return "/paciente/index";
				} else if(user.getClass().toString().substring(23).contains("AtendenteARS")) {
					sessionhttp.setAttribute("usuarioTipo", "2");
					return "/ars/index";
				}else if(user.getClass().toString().substring(23).contains("MedicoClinica")) {
					sessionhttp.setAttribute("usuarioTipo", "3");
					return "/medico/index";
				} else if(user.getClass().toString().substring(23).contains("GerenteARS")) {
					sessionhttp.setAttribute("usuarioTipo", "4");
					return "/ars/index";
				} else if(user.getClass().toString().substring(23).contains("AtendenteInstituicao")) {
					sessionhttp.setAttribute("usuarioTipo", "5");
					return "/instituicao/index";
				}else if(user.getClass().toString().substring(23).contains("GerenteInstituicao")) {
					sessionhttp.setAttribute("usuarioTipo", "5");
					return "/instituicao/index";
				}
				
				
				return "/index";
			} else {
				System.out.println("erro de senha");
				FacesMessage fm = new FacesMessage("Erro");
				fm.setSeverity(FacesMessage.SEVERITY_ERROR);
				fc.addMessage(null, fm);
				this.setSenha(null);
				return "/login";
			}
			
		}
		
		public String registraSaida() {
			FacesContext fc = FacesContext.getCurrentInstance();
			ExternalContext ec = fc.getExternalContext();
			HttpSession sessionhttp = (HttpSession) ec.getSession(false);
			sessionhttp.removeAttribute("usuarioCPF");
			sessionhttp.removeAttribute("usuarioTipo");
			ec.invalidateSession();
			return "/login";
		}

		public String getUsuarioCPF() {
			return usuarioCPF;
		}

		public void setUsuarioCPF(String usuarioCPF) {
			this.usuarioCPF = usuarioCPF;
		}

		public String getSenha() {
			return senha;
		}

		public void setSenha(String senha) {
			this.senha = senha;
		}
		
		private EntityManager getManager() {
			FacesContext fc = FacesContext.getCurrentInstance();
			ExternalContext ec = fc.getExternalContext();
			HttpServletRequest request = (HttpServletRequest) ec.getRequest();
			return (EntityManager) request.getAttribute("EntityManager");

		}

		public UserSessionBean getSession() {
			return session;
		}

		public void setSession(UserSessionBean session) {
			this.session = session;
		}
		
		
		
 }
